package com.yidu.BusinessDealWith.service.impl;

import com.yidu.BusinessData.entity.SecurityEntity;
import com.yidu.BusinessDealWith.entity.*;
import com.yidu.BusinessDealWith.mapper.InventoryMapper;
import com.yidu.BusinessDealWith.service.InventoryService;
import com.yidu.StockControl.entity.*;
import com.yidu.StockControl.mapper.*;
import com.yidu.StockControl.service.*;
import com.yidu.Util.util.Dbutil;
import com.yidu.Util.util.TableIdUtil;
import com.yidu.Util.util.TableNameUtil;
import com.yidu.Util.util.TableTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**@description: 庫存統計Impl
 *@author: 肖理
 *@create: 2022-09-10
 **/
import java.util.ArrayList;
import java.util.List;
@Service
public class InventoryServiceImpl implements InventoryService {
    //调用五大库存Mapper与工具类
    @Autowired
    InventoryMapper inventoryMapper;
    @Autowired
    CashInventoryMapper cashInventoryMapper;
    @Autowired
    CashInventorysMapper cashInventorysMapper;
    @Autowired
    SecurityInventoryMapper securityInventoryMapper;
    @Autowired
    SecurityInventorsMapper securityInventorsMapper;
    @Autowired
    TAInventoryMapper taInventoryMapper;
    @Autowired
    Dbutil dbutil;

    /**
     * 库存统计处理
     * @param id 编号
     * @param fundId 基金编号
     * @param dateTime 统计时间
     * @return
     */
    @Override
    public List<InventoryEntity> select(String id,String fundId,String dateTime,String userName) {
        List inventoryEntityList = new ArrayList();
        InventoryEntity inventoryEntity = new InventoryEntity(1,"证券库存","","","",0,"未统计");
        InventoryEntity inventoryEntity2 = new InventoryEntity(2,"现金库存","","","",0,"未统计");
        InventoryEntity inventoryEntity3 = new InventoryEntity(3,"Ta库存","","","",0,"未统计");
        InventoryEntity inventoryEntity4 = new InventoryEntity(4,"证券应收应付库存","","","",0,"未统计");
        InventoryEntity inventoryEntity5 = new InventoryEntity(5,"现金应收应付库存","","","",0,"未统计");
        if (id !=null && !id.equals("")) {
            String[] ids = null;
            ids = id.split(",");

            for (String myId:ids) {
                switch (myId){
                    // 证券库存统计数据
                    case "1":
                        List<InventorySecurityInventorysEntity> selectzk = inventoryMapper.selectzk(fundId, dateTime);
                         inventoryEntity = new InventoryEntity(1,"证券库存",fundId,userName,dateTime,selectzk.size(),"已统计");
                        if (selectzk.size()!=0&&selectzk.get(0)!=null){
                            for (InventorySecurityInventorysEntity securityInventorysEntity:selectzk) {
                                int i = securityInventorsMapper.deleteSecurityInventorsDate(securityInventorysEntity.getSecurityInventorsId(),fundId, dateTime);
                                    String maxId = dbutil.getMaxId3(TableIdUtil.SSID, TableNameUtil.SIS, TableTimeUtil.SSTime, dateTime);
                                    SecurityInventors securityInventors = new SecurityInventors();
                                    securityInventors.setSecurityInventorsId(maxId);
                                    securityInventors.setSecurityInventorsTime(dateTime);
                                    securityInventors.setFundId(fundId);
                                    securityInventors.setSecurityId(securityInventorysEntity.getSecurityId());
                                    securityInventors.setSecurityPrice(securityInventorysEntity.getSecurityPrice());
                                    securityInventors.setSecurityNum(securityInventorysEntity.getSecurityNum());
                                    securityInventors.setSecurityMoney(securityInventorysEntity.getSecurityMoney());
                                    securityInventorsMapper.insertSecurityInventors(securityInventors);
                            }
                        }
                        break;
                    case "2":
                        //现金库存统计数据
                        List<InventoryCashInventorysEntity> selectxk = inventoryMapper.selectxk(fundId, dateTime);
                        inventoryEntity2 = new InventoryEntity(2,"现金库存",fundId,userName,dateTime,selectxk.size(),"已统计");
                        if (selectxk.size()!=0&&selectxk.get(0)!=null){
                            System.out.println("你好");
                            for (InventoryCashInventorysEntity inventorysEntity :selectxk) {
                                System.out.println(inventorysEntity);
                                int i = cashInventorysMapper.deleteCashInventorysDate(inventorysEntity.getCashInventorysId(),fundId, dateTime);
                                    String maxId = dbutil.getMaxId3(TableIdUtil.CSID, TableNameUtil.CIS, TableTimeUtil.CSTime, dateTime);
                                    CashInventorys cashInventorys = new CashInventorys();
                                    cashInventorys.setCashInventorysId(maxId);
                                    cashInventorys.setFundId(fundId);
                                    cashInventorys.setCashInventorysTime(dateTime);
                                    cashInventorys.setCashId(inventorysEntity.getCashId());
                                    cashInventorys.setAccounName(inventorysEntity.getAccounName());
                                    cashInventorys.setBalance(inventorysEntity.getBalance());
                                    cashInventorysMapper.insertCashInventorys(cashInventorys);
                            }
                        }
                        break;
                    case "3":
                        //Ta库存统计数据
                        List<InventoryTaEntity> selectt = inventoryMapper.selectt(fundId, dateTime);
                        inventoryEntity3 = new InventoryEntity(3,"Ta库存",fundId,userName,dateTime,selectt.size(),"已统计");
                        if (selectt.size()!=0&&selectt.get(0)!=null) {
                            for (InventoryTaEntity taEntity : selectt) {
                                   taInventoryMapper.deleteTAInventoryfDate(taEntity.getTaInventoryId(),fundId, dateTime);
                                    String maxId = dbutil.getMaxId3(TableIdUtil.TASID, TableNameUtil.TAI, TableTimeUtil.TASTime, dateTime);
                                    TAInventory taInventory = new TAInventory();
                                    taInventory.setTaInventoryId(maxId);
                                    taInventory.setFundId(fundId);
                                    taInventory.setStatisticsDate(dateTime);
                                    taInventory.setTaMoney(taEntity.getTaMoney());
                                    taInventory.setNumber(taEntity.getNumber());
                                    taInventoryMapper.insertTAInventory(taInventory);
                            }
                        }
                        break;
                    case "4":
                        //证券应收应付统计数据
                        List<InventorySecurityInventoryEntity> selectz = inventoryMapper.selectz(fundId, dateTime);
                        inventoryEntity4 = new InventoryEntity(4,"证券应收应付库存",fundId,userName,dateTime,selectz.size(),"已统计");
                        System.out.println(selectz.size());
                        if (selectz.size()!=0&&selectz.get(0)!=null){
                            for (InventorySecurityInventoryEntity securityInventoryEntity:selectz) {
                                System.out.println(securityInventoryEntity);
                                int i = securityInventoryMapper.deleteSecurityInventoryDate(securityInventoryEntity.getSecurityInventoryId(),fundId, dateTime);
                                String maxId = dbutil.getMaxId3(TableIdUtil.SASD, TableNameUtil.SI, TableTimeUtil.SASTime,dateTime);
                                SecurityInventory securityInventory = new SecurityInventory();
                                securityInventory.setSecurityInventoryId(maxId);
                                securityInventory.setFundId(fundId);
                                securityInventory.setSecurityId(securityInventoryEntity.getSecurityId());
                                securityInventory.setSecurityName(securityInventoryEntity.getSecurityName());
                                securityInventory.setBusinessStatu(securityInventoryEntity.getBusinessStatu());
                                securityInventory.setCashId(securityInventoryEntity.getCashId());
                                securityInventory.setSecurityInventoryTime(dateTime);
                                securityInventory.setSecurityTotalAmount(securityInventoryEntity.getSecurityTotalAmount());
                                securityInventory.setSecurityType(3);
                                securityInventoryMapper.insertSecurityInventory(securityInventory);
                            }
                        }
                        break;
                    case "5":

                        List<InventoryCashInventoryEntity> selectx = inventoryMapper.selectx(fundId, dateTime);
                        System.out.println(selectx);
                        inventoryEntity5 = new InventoryEntity(5,"现金应收应付库存",fundId,userName,dateTime,selectx.size(),"已统计");
                        if (selectx.size()!=0&&selectx.get(0)!=null) {
                            for (InventoryCashInventoryEntity cashInventoryEntity : selectx) {
                                int i = cashInventoryMapper.deleteCashInventoryDate(cashInventoryEntity.getCashInventoryId(),fundId, dateTime);
                                    String maxId = dbutil.getMaxId3(TableIdUtil.CASID, TableNameUtil.CI, TableTimeUtil.CASTime, dateTime);
                                    CashInventory cashInventory = new CashInventory();
                                    cashInventory.setCashInventoryId(maxId);
                                    cashInventory.setFundId(fundId);
                                    cashInventory.setCashId(cashInventoryEntity.getCashId());
                                    cashInventory.setCashInventoryTime(dateTime);
                                    cashInventory.setBusinessStatu(cashInventoryEntity.getBusinessStatu());
                                    cashInventory.setBusinessType(3);
                                    cashInventory.setCashTotalAmount(cashInventoryEntity.getCashTotalAmount());
                                    cashInventoryMapper.insertCashInventory(cashInventory);
                            }
                        }
                        break;
                }
            }
        }
        inventoryEntityList.add(inventoryEntity);
        inventoryEntityList.add(inventoryEntity2);
        inventoryEntityList.add(inventoryEntity3);
        inventoryEntityList.add(inventoryEntity4);
        inventoryEntityList.add(inventoryEntity5);
        return inventoryEntityList;
    }

    @Override
    public List<InventorySecurityInventoryEntity> selectz(String fundId, String dateTime) {
        return inventoryMapper.selectz(fundId,dateTime);
    }

    @Override
    public List<InventoryCashInventoryEntity> selectx(String fundId, String dateTime) {
        return inventoryMapper.selectx(fundId,dateTime);
    }

    @Override
    public List<InventoryCashInventorysEntity> selectxk(String fundId, String dateTime) {
        return inventoryMapper.selectxk(fundId,dateTime);
    }

    @Override
    public List<InventorySecurityInventorysEntity> selectzk(String fundId, String dateTime) {
        return inventoryMapper.selectzk(fundId,dateTime);
    }

    @Override
    public List<InventoryTaEntity> selectt(String fundId,String dateTime) {
        return inventoryMapper.selectt(fundId,dateTime);
    }
}
